import java.util.*;
import java.io.*;


public class GenerateUserDirectoryList {

    private static RandomAccessFile outputFile;
    private static Vector proVector = new Vector ();
    private static String serverDir  ="C:/myProjects/apache2/htdocs/";
    private static String userDir    ="D:/myServers/ApacheGroup/Tomcat4/webapps/index/user/htdocs/";
    private static String baseDir    = null;
    private static String pdir_id    = null;
    private static String dir_id    = null;
    private static String currentDir = null;
    private static String systemDir  = null;
    private static String htmlDir    = null;
    private static String jspDir     = null;

    public static void main( String args[] )
    {
	boolean full_list = false;
	if(args.length != 2) {
	    System.out.println("==========================================================");
	    System.out.println("[1][SUBDIR][Generate htdocs(/) or subdir using _profile]");
	    System.out.println("[2][SUBDIR][Generate htdocs(/) or full  sidebar]");
	    System.out.println("==========================================================");
	    System.exit(0);
	}
	else if (args[0].equals("2")) {
	    full_list = true;
	}
	else {
	    full_list = false;
	}

	if (args[1].equals("/")) {
	    baseDir = serverDir;
	    pdir_id="3wcenter";
	    dir_id=null;
	}
	else {
	    /*
	    currentDir = args[1];
	    baseDir =serverDir+currentDir;
	    if (currentDir.indexOf('/') != -1) {
		pdir_id= currentDir.substring(0, currentDir.indexOf('/'));
		dir_id = currentDir.substring(currentDir.indexOf('/'));
	    }
	    else {
	    */
	    userDir=userDir+"/"+currentDir+"/";
	    pdir_id = currentDir;
	    dir_id =null;
	}

	System.out.println("pdir_id="+pdir_id);
	System.out.println("dir_id="+dir_id);

	File f_dir = new File (baseDir);

	if (!f_dir.isDirectory()) {
	    System.out.println(baseDir+" is not a directory!");
	    System.exit(0);
	}

	GenerateUserDirectoryList maker = new GenerateUserDirectoryList();

	systemDir  = userDir + "/_system/";
	htmlDir    = systemDir + "html/";
	jspDir     = systemDir + "javaScript/";

	File dir = new File (baseDir);
	//dir.mkdir();
	
	if(dir.isDirectory()) {
	    System.out.println("Directory:"+dir.getName()+" found!");
	}
	else {
	    System.out.println("Directory:"+dir.getName()+" not found!");
	    System.exit(0);
	}
	
	maker.checkSystemDirectory(systemDir);
	maker.checkSystemDirectory(htmlDir);
	maker.checkSystemDirectory(jspDir);

	//System.exit(0);

	maker.createLeftBarHtmlFile();
	maker.loadLeftBarProfile();
	maker.addHtmlHeadToLeftBar();
	maker.addHtmlProfileToLeftBar();
	if (full_list) {
	    maker.addHtmlBodyToLeftBar();
	}
	maker.addHtmlEnd();
	maker.closeFile(outputFile);
    }

 
    private void createLeftBarHtmlFile()
    {
	File file_name = null;
	//check if it is htdocs index
	
	if (baseDir.equals(serverDir)) {
	    file_name = new File (htmlDir, "index_left.html");
	}
	else {
	    file_name = new File (baseDir, "index.html");
	}
	
	try {
	    if (file_name.exists()) {
		System.out.println("deleting old index_left.html file .....");
		if(file_name.delete()) {
		    System.out.println("Old index_left.html file has been deleted");
		}
	    }
	    outputFile = new RandomAccessFile( file_name, "rw" );

	}
	catch ( IOException e ) {
	}
    }

    private void loadLeftBarProfile() {

	File check_profile = new File (baseDir, "_profile.txt");

	if (check_profile.exists()) {
	    ProfileLoader loader = new ProfileLoader();
	    loader.setFileName(baseDir+"/_profile.txt");
	    loader.loadProfile();
	    proVector = loader.getVector();
	}
	else {
	    System.out.println("No profile for this directory");
	}

    }

    public void addHtmlHeadToLeftBar( )
    {

       try {

	   outputFile.writeBytes("<HTML><HEAD><TITLE>Direcotry"+currentDir+"Index</TITLE></HEAD>");
	   outputFile.writeBytes("<BODY>");
      
	   if (baseDir.equals(serverDir)) {
	       outputFile.writeBytes("<TABLE border='0' width='300' height='100'>");
	       outputFile.writeBytes("<TR><TD><IMG height ='100' width='100' SRC=/_system/images/3wc.JPG>");
	       outputFile.writeBytes("</TD></TR><BR>");
	       outputFile.writeBytes("<BR><TR><TD>");	      
	       outputFile.writeBytes("<FORM method=post action=/index/servlet/SideBarEditServlet target=mainFrame>");
	       outputFile.writeBytes("<input type=submit value=EDIT name=B3><input type=hidden name=dir_id value=3wcenter>");
	       outputFile.writeBytes("</FORM></TD></TR><BR><BR></TABLE>");
	   }
	   else {
	       outputFile.writeBytes("<TABLE width=300><TR><TD>");
	       outputFile.writeBytes("<a href=/index/servlet/SideBarServlet>");
	       outputFile.writeBytes("3WCenter</a></TD></TR>");
	       outputFile.writeBytes("<tr><td><img src=/_system/images/L.gif>");
	       outputFile.writeBytes(pdir_id.toUpperCase()+"</TD></TR>");

	       if (!dir_id.equals("")) {
		   outputFile.writeBytes("<tr><td><IMG src=/_system/images/L.gif>");
		   outputFile.writeBytes(dir_id.toUpperCase()+"</TD></TR>");
	       }
	       
	       outputFile.writeBytes("<BR><TR><TD><a href=/index/servlet/SideBarEditServlet?pdir_id="+pdir_id+
				     "&dir_id="+dir_id+" target=mainFrame>");
	       outputFile.writeBytes("<img border=0 src=/_system/images/edit.gif></a></TD></TR><BR><BR>");
	   }
	   outputFile.writeBytes("</TABLE><BR><BR>\n<SIDEBAR>\n");
       }
       catch ( IOException io ) {
       }

    }

    public void addHtmlProfileToLeftBar()
    {

	/**
	 * add user profile into side bar
	 **/
	String    url_name   = null;
	String    url_adds   = null;
	int       i          = 0;
	try {
	    Enumeration enum = proVector.elements();  
	    while (enum.hasMoreElements()) {
		url_name = (String)enum.nextElement();
		url_adds = (String)enum.nextElement();
		outputFile.writeBytes("\n<SHOW id="+i+"><A href =/index/servlet/SideBarServlet?pdir_id="+url_adds.replaceAll("/","")+">"+
				      url_name.toUpperCase()+"</A></SHOW><BR>\n");
		i++;
	    }
	}
	catch ( IOException io ) {
	}
    }
	
    public void addHtmlBodyToLeftBar()
    {
	/**
	 * add directories list into side bar
	 **/

	String   sub_dir       = null;
	String[] file_list     = null;

	file_list = getDirList(baseDir);

	for (int n = 0; n < file_list.length; n++) {

	    File f_file_list = new File (baseDir, file_list[n]);
	    
	    if (f_file_list.isDirectory()) {
		sub_dir = file_list[n];
		System.out.println("subDir="+sub_dir);

		if(!sub_dir.startsWith("_")) {

		    /**
		     * add sub directory name into side bar
		     */

		    try {
			outputFile.writeBytes("<A href=/index/servlet/SideBarServlet?pdir_id="+
					      currentDir+"&dir_id="+sub_dir+">");
			outputFile.writeBytes(sub_dir+"</A><BR>");
		    }
		    catch ( IOException io ) {
		    }
		}
	    }
	}
    }

    public void addHtmlEnd( )
    {
	try {
	    outputFile.writeBytes("\n</SIDEBAR>\n</BODY></HTML>");
	}
	catch ( IOException io ) {
	}
    }

    private void closeFile(RandomAccessFile raf) 
    {
	try {
	    raf.close();
	}
	catch( IOException ex ) {
	    System.exit( 1 );
	}
    }

    public String[] getDirList(String dir)
    {
	File dirName = new File (dir);
	return(dirName.list());
    }

 
    private void checkSystemDirectory (String sub_dir) {
	File sub_d_f = new File(sub_dir);
	if(sub_d_f.isDirectory()) {
	    System.out.println("Directory:"+sub_d_f.getName()+" found!");
	}
	else {
	    System.out.println("Directory:"+sub_d_f.getName()+" not found!");
	    sub_d_f.mkdirs();
	    System.out.println("Directory:"+sub_d_f.getName()+" has been created!");
	}
    }
}



